package cn.nkk.hikvision.callBack;

import cn.nkk.hikvision.sdk.HCNetSDK;
import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * 回调
 */

@Component
public class DVRMessageCallBackV31 implements HCNetSDK.FMSGCallBack_V31 {


    private final static Logger log = LoggerFactory.getLogger(DVRMessageCallBackV31.class);

    @Override
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        switch (lCommand) {
            case HCNetSDK.COMM_ITS_PLATE_RESULT:
//                交通抓拍结果(新报警信息)
                log.info("【海康车牌摄像头回调】交通抓拍的终端图片上传");
                HCNetSDK.NET_ITS_PLATE_RESULT strItsPlateResult = new HCNetSDK.NET_ITS_PLATE_RESULT();
                strItsPlateResult.write();
                Pointer pItsPlateInfo = strItsPlateResult.getPointer();
                pItsPlateInfo.write(0, pAlarmInfo.getByteArray(0, strItsPlateResult.size()), 0, strItsPlateResult.size());
                strItsPlateResult.read();
                try {
                    String license = new String(strItsPlateResult.struPlateInfo.sLicense, "GBK");
                    // 车型识别：0- 未知，1- 客车(大型)，2- 货车(大型)，3- 轿车(小型)，4- 非机动车
                    String host = new String(pAlarmer.sDeviceIP).trim();//ip地址
                    String type = strItsPlateResult.byVehicleType + "".trim();
                    String plateNumber = license.substring(1).trim();
                    String byCountry = license.substring(1, 2).trim();
                    String byColor = license.substring(0, 1).trim();
                    log.info("【海康车牌摄像头回调】ip地址: {}", host);
                    log.info("【海康车牌摄像头回调】车辆类型: {}", type);
                    log.info("【海康车牌摄像头回调】车牌号: {}", plateNumber);
                    log.info("【海康车牌摄像头回调】车牌省份: {}", byCountry);
                    log.info("【海康车牌摄像头回调】车牌颜色: {}", byColor);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            case HCNetSDK.COMM_UPLOAD_PLATE_RESULT:
                log.info("【海康车牌摄像头回调】交通抓拍结果上传");
                break;
//                COMM_VEHICLE_RECOG_RESULT
            case HCNetSDK.COMM_ALARM:
                log.info("【海康车牌摄像头回调】8000报警信息主动上传");
                break;
            case HCNetSDK.COMM_ITS_PARK_VEHICLE:
                log.info("【海康车牌摄像头回调】停车场数据上传");
                break;
            case HCNetSDK.COMM_VEHICLE_CONTROL_ALARM:
                log.info("【海康车牌摄像头回调】车辆报警上传");
                break;
        }
        return true;
    }
}
